package com.stx.jeepay.manager.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.lang.UUID;
import com.alibaba.fastjson.JSONObject;
import com.stx.jeepay.core.cache.Cache;
import com.stx.jeepay.core.dto.LoginParams;
import com.stx.jeepay.core.enums.CS;
import com.stx.jeepay.core.expection.BizException;
import com.stx.jeepay.core.model.ApiRes;
import com.stx.jeepay.manager.service.AuthService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Api(tags = "认证模块")
@RestController
@RequestMapping("/api/anon/auth")
public class AuthController extends CommonCtrl {

    @Autowired
    private Cache cache;

    @Autowired
    private AuthService authService;

    /**
     * 用户信息认证 获取iToken
     **/
    @ApiOperation("登录认证")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ia", value = "用户名 i account, 需要base64处理", required = true),
            @ApiImplicitParam(name = "ip", value = "密码 i passport,  需要base64处理", required = true),
            @ApiImplicitParam(name = "vc", value = "证码 vercode,  需要base64处理", required = true),
            @ApiImplicitParam(name = "vt", value = "验证码token, vercode token ,  需要base64处理", required = true)
    })
    @PostMapping("/validate")
    public ApiRes validate(@RequestBody LoginParams params) {
        String account = Base64.decodeStr(params.getIa());  //用户名 i account, 已做base64处理
        String ipassport = Base64.decodeStr(params.getIp());    //密码 i passport,  已做base64处理
        String vercode = Base64.decodeStr(params.getVc());    //验证码 vercode,  已做base64处理
        String vercodeToken = Base64.decodeStr(params.getVt());    //验证码token, vercode token ,  已做base64处理

        String cacheCode = cache.getString(CS.getCacheKeyImgCode(vercodeToken));
        if (StringUtils.isEmpty(cacheCode) || !cacheCode.equalsIgnoreCase(vercode)) {
            throw new BizException("验证码有误！");
        }

        // 返回前端 accessToken
        String accessToken = authService.auth(account, ipassport);

        // 删除图形验证码缓存数据
        cache.del(CS.getCacheKeyImgCode(vercodeToken));

        return ApiRes.ok4newJson(CS.ACCESS_TOKEN_NAME, accessToken);
    }

    @ApiOperation("图片验证码")
    @GetMapping("/vercode")
    public ApiRes vercode() throws BizException {
        //定义图形验证码的长和宽 // 4位验证码
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(137, 40, 4, 80);
        lineCaptcha.createCode(); // 生成图片

        // 验证码存Redis
        String vercodeToken = UUID.fastUUID().toString();
        cache.setString(CS.getCacheKeyImgCode(vercodeToken), lineCaptcha.getCode(), CS.VERCODE_CACHE_TIME);
        JSONObject result = new JSONObject();
        result.put("imageBase64Data", lineCaptcha.getImageBase64Data());
        result.put("vercodeToken", vercodeToken);
        result.put("expireTime", CS.VERCODE_CACHE_TIME);

        return ApiRes.ok(result);
    }
}
